<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="机构名称" prop="orgName">
        <el-input
          v-model="queryParams.orgName"
          placeholder="请输入机构名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="编辑人员" prop="createBy">
        <el-select v-model="queryParams.auditBy" filterable clearable placeholder="请选择编辑人员">
          <el-option
            v-for="item in userOptions"
            :key="item.userId"
            :label="item.userName"
            :value="item.userId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="发布时间" prop="publishTimeStr">
        <el-date-picker clearable
                        v-model="queryParams.publishTimeStr"
                        value-format="yyyy-MM-dd"
                        type="date"
                        placeholder="请选择发布时间">
        </el-date-picker>
      </el-form-item>

      <el-form-item label="审核人员" prop="auditBy">
        <el-select v-model="queryParams.updateBy" filterable clearable placeholder="请选择审核人员">
          <el-option
            v-for="item in userOptions"
            :key="item.userId"
            :label="item.userName"
            :value="item.userId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="修改时间" prop="updateTimeStr">
        <el-date-picker clearable
                        v-model="queryParams.updateTimeStr"
                        value-format="yyyy-MM-dd"
                        type="date"
                        placeholder="请选择修改时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="覆盖范围" prop="areaId">
        <el-cascader
          v-model="queryParams.parkAreaId"
          placeholder="请选择覆盖范围"
          :options="parkAreaOptions"
          :props="{ value: 'id',checkStrictly: true,emitPath:false}"
          filterable
          clearable
        ></el-cascader>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['org:org:add']"
        >新增
        </el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orgList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="名称" align="center" prop="orgName"/>
      <el-table-column label="上级单位" align="center" prop="parentOrgName"/>
      <el-table-column label="覆盖地区" align="center" prop="areaName"/>
      <el-table-column label="详细地址" align="center" prop="address"/>
      <el-table-column label="联系方式" align="center" prop="tel"/>
      <el-table-column label="编辑人员" align="center" prop="auditName"/>
      <el-table-column label="发布时间" align="center" prop="publishTimeStr"/>
      <el-table-column label="审核人员" align="center" prop="auditName"/>
      <el-table-column label="修改时间" align="center" prop="updateTime"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['org:org:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['org:org:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改主管单位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="机构名称" prop="orgName">
          <el-input v-model="form.orgName" placeholder="请输入机构名称"/>
        </el-form-item>
        <el-form-item label="曾用名" prop="orgName">
          <el-input v-model="form.formerName" placeholder="请输入曾用名"/>
        </el-form-item>

        <el-row>
          <el-col :span="12">
            <el-form-item label="所属机构" prop="parentOrgId">
              <!--              <el-select v-model="form.parentOrgId" filterable @change="orgChange" placeholder="请选择所属机构">-->
              <!--                <el-option-->
              <!--                  v-for="item in orgOptions"-->
              <!--                  :key="item.orgId"-->
              <!--                  :label="item.orgName"-->
              <!--                  :value="item.orgId"-->
              <!--                ></el-option>-->
              <!--              </el-select>-->
              <treeselect v-model="form.parentOrgId" :options="orgOptions" @change="orgChange" :show-count="true"
                          placeholder="请选择主管部门"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="机构类型" prop="orgType">
              <el-radio-group v-model="form.orgType">
                <el-radio label="1">内设机构</el-radio>
                <el-radio label="2">直属机构</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="主管类型" prop="type">
              <el-select v-model="form.type" placeholder="请选择主管类型">
                <el-option
                  v-for="dict in dict.type.org_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <el-radio-group v-model="form.status">
                <el-radio label="1">有效</el-radio>
                <el-radio label="0">无效</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
          <el-form-item label="覆盖地区" prop="parkAreaIds">
            <!--          <treeselect v-model="form.area" :options="areaOptions" :show-count="true" placeholder="请选择覆盖地区"/>-->
            <el-cascader style="width: 800px;"
                         v-model="form.parkAreaIds"
                         placeholder="请选择覆盖地区"
                         :options="parkAreaOptions"
                         :props="{ value: 'id', checkStrictly: true}"
                         filterable></el-cascader>
          </el-form-item>
          <!--        <el-form-item label="标签" prop="tags">-->
          <!--          <el-cascader-->
          <!--            v-model="form.tags"-->
          <!--            placeholder="请选择标签"-->
          <!--            :options="tagOptions"-->
          <!--            :props="{ multiple: true,emitPath: false }"-->
          <!--            filterable></el-cascader>-->
          <!--        </el-form-item>-->
          <el-form-item label="原文路径">
            <el-input
              v-model="form.navigation"
              type="textarea"
              placeholder="请输入原文路径"
            />
          </el-form-item>
          <el-form-item label="原文网址" prop="fromUrl">
            <el-input v-model="form.fromUrl" placeholder="请输入来源地址"/>
          </el-form-item>
          <el-form-item label="单位地址" prop="address">
            <el-input v-model="form.address" placeholder="请输入单位地址"/>
          </el-form-item>

          <el-row>
            <el-col :span="12">
              <el-form-item label="负责人" prop="principal">
                <el-input v-model="form.principal" placeholder="请输入负责人"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="联系方式" prop="tel">
                <el-input v-model="form.tel" placeholder="请输入联系方式"/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="网站地址" prop="webUrl">
            <el-input v-model="form.webUrl" placeholder="请输入网站地址"/>
          </el-form-item>

          <el-form-item label="主要职能" prop="mainFunctions">
            <editor v-if="open" v-model="form.mainFunctions" :min-height="192"/>
          </el-form-item>

          <el-form-item label="是否发布" prop="isPublish">
            <el-radio-group v-model="form.isPublish">
              <el-radio label="1">是</el-radio>
              <el-radio label="0">否</el-radio>
            </el-radio-group>
          </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listOrg, getOrg, delOrg, addOrg, updateOrg, areaTree, tagSelect, getAllOrg} from "@/api/org/org";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {getAllUser} from "@/api/system/user";
import {parkAreaTree} from "@/api/area/parkArea";

export default {
  name: "Org",
  dicts: ['org_type'],
  components: {Treeselect},
  data() {
    return {
      // 遮罩层
      loading: true,
      props: {multiple: true},
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 主管单位表格数据
      orgList: [],
      // 地区树
      parkAreaOptions: undefined,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      orgOptions: [],
      // 标签类型树
      tagTypeOptions: [],
      // 人员下拉
      userOptions: [],
      // 标签下拉项
      tagOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orgName: null,
        auditBy: '',
        updateBy: '',
        publishTimeStr: '',
        updateTimeStr: '',
        parkAreaId: '',
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
    this.getAll();
    this.parkAreaTree();
    this.tagSelect();
    this.getAllUser();
  },
  methods: {
    /** 查询主管单位列表 */
    getList() {
      this.loading = true;
      listOrg(this.queryParams).then(response => {
        this.orgList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getAll() {
      getAllOrg().then(response => {
        this.orgOptions = response.data;
      });
    },
    getAllUser() {
      getAllUser().then(response => {
        this.userOptions = response.data;
      });
    },
    /** 查询园区列表 */
    parkAreaTree() {
      parkAreaTree().then(response => {
        this.parkAreaOptions = response.data;
      });
    },
    tagSelect() {
      tagSelect().then(response => {
        this.tagOptions = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        orgId: null,
        orgName: null,
        formerName: null,
        parentOrgId: null,
        type: null,
        area: null,
        parkAreaIds: null,
        status: null,
        tags: [],
        navigation: null,
        fromUrl: null,
        address: null,
        tel: null,
        mainFunctions: null,
        principal: null,
        webUrl: null,
        orgType: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        tagType: null,
        isPublish: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.orgId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加主管单位";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const orgId = row.orgId || this.ids
      getOrg(orgId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改主管单位";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.orgId != null) {
            updateOrg(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrg(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
          this.getAll();
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const orgIds = row.orgId || this.ids;
      this.$modal.confirm('是否确认删除主管单位编号为"' + orgIds + '"的数据项？').then(function () {
        return delOrg(orgIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('org/org/export', {
        ...this.queryParams
      }, `org_${new Date().getTime()}.xlsx`)
    },
    orgChange(item) {
      // const obj = this.orgOptions.filter(org => org.orgId === item);
      // this.form.type = (obj[0].type);
      // this.form.address = (obj[0].address);
      // this.form.area = (obj[0].area);
      // this.form.tel = (obj[0].tel)
      if (item == '') {
        this.form.orgType = '';
      }
    },
    tagTypeChange(item) {
      const tagTypes = [];

      if (item) {
        item.forEach(typeTemp1 => {
          for (let i = 0; i < typeTemp1.length; i++) {
            if (!tagTypes.includes(typeTemp1[i])) {
              tagTypes.push(typeTemp1[i]);
            }
          }
        });

        tagSelect({tagTypes: tagTypes}).then(response => {
          this.tagOptions = response.data;
        });
      }
    },
  }
};
</script>
